package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;

public class PeoplerailNewsAnalyse implements AnalyseNews {
/**
 * http://www.peoplerail.com/rail/show-456-168750-1.html   新闻
 * http://szb.peoplerail.com/shtml/rmtdb/20140107/104346.shtml  平媒
 */
	//http://www.peoplerail.com/railcms/html/2013/shehuixw_0506/133821.html
	//http://www.peoplerail.com/railcms/show-229-134605-1.html
	private static final String[] regex={
		"http://www.peoplerail.com/rail/show-[0-9]*-[0-9]*-1.html",
		"http://szb.peoplerail.com/shtml/rmtdb/[0-9]{8}/[0-9]*.shtml"
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);




		String title=null;
		String content=null;
		String author=null;
		Long date=null;

		doc.select("#menu").remove();
		title=doc.select("#article h2").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("div.f-20").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("h2#bt").text();
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("#article div.pt30"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.f-14"));
		}

		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("span.c6d").text();
		if(StringUtils.isBlank(authorStr)){
			authorStr=doc.select("div#laiyuan").text();
		}
		author=JSoupUtils.matchAuthor(doc, "来源:");

		date=DateUtils.matchDate(doc.select("span#shijian").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select("span.c6d").text());
			if (date == null) {
				date=DateUtils.matchDate(doc.select("div#shijian").text());
				if (date == null) {
					date=JSoupUtils.matchDate(doc,"时间:");
				}
			}
		}
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		if (StringUtils.isBlank(news.getTitle())||news.getDate() == null) {
		}
		return news;
	}

	public static void main(String[] args) {
		String url="http://www.peoplerail.com/rail/show-484-750-1.html";
		PeoplerailNewsAnalyse test=new PeoplerailNewsAnalyse();
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
